package com.redstars.tool.screw.utils;

import cn.hutool.core.io.IoUtil;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author : zhouhx
 * @date : 2023/3/31 10:40
 */
public class ServletUtils {
    /**
     *
     * @author zhuohx
     * @description 写入文件
     * @parms  [response, filename 文件名, content 文件内容]
     * @return void 
     * @throws 
     * @date 2023/3/31 10:41
     */
    public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
        // 设置 header 和 contentType
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        // 输出附件
        IoUtil.write(response.getOutputStream(), false, content);
    }
}
